home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_exim.idb / usr / freeware / bin / exigrep.z / exigrep
Encoding:
Text File  |  1999-01-26  |  1.7 KB  |  58 lines

  1. #! /usr/bin/perl -w
  2.  
  3. # Copyright (c) 1997 University of Cambridge.
  4. # See the file NOTICE for conditions of use and distribution.
  5.  
  6. # This is a perl script which extracts from an Exim log all entries
  7. # for all messages that have an entry that matches a given pattern.
  8. # If *any* entry for a particular message matches the pattern, *all*
  9. # entries for that message are displayed.
  10.  
  11. # There must be one argument, which is the pattern. Subsequent arguments
  12. # are the files to scan; if none, the standard input is read.
  13.  
  14. # Extract the pattern and make sure any relevant characters are quoted if
  15. # the -l flag is given.
  16.  
  17. shift @ARGV if ($literal = ($#ARGV >= 0 && $ARGV[0] eq "-l"));
  18.  
  19. die "usage: exigrep [-l] <pattern> [<log file>]...\n" if ($#ARGV < 0);
  20.  
  21. $pattern = shift @ARGV;
  22. $pattern =~ s/\W/\\$&/g if $literal;
  23.  
  24. # Loop reading the input. We buffer up information on a per-message basis.
  25. # It is done this way rather than reading the input twice in order that the
  26. # input can be a pipe. Program defensively against short lines finding
  27. # their way into the log.
  28.  
  29. while (<>)
  30.   {
  31.   next if length($_) < 20;
  32.   $id = substr($_, 20, 16);
  33.   next if $id !~ /\w{6}\-\w{6}\-\w{2}/;
  34.   $saved{$id} = '' unless defined($saved{$id});
  35.  
  36.   # Save up the data for this message in case it becomes interesting later.
  37.   $saved{$id} .= $_;
  38.  
  39.   # Are we interested in this id ?
  40.   $id_list{$id} = 1 if /$pattern/io;
  41.  
  42.   # See if this is a completion for some message. If it is interesting,
  43.   # print it, but in any event, throw away what was saved.
  44.  
  45.   if (/Completed$/)
  46.     {
  47.     delete $id_list{$id}, print "$saved{$id}\n" if $id_list{$id};
  48.     delete $saved{$id};
  49.     }
  50.   }
  51.  
  52. # Print the unCompleted data
  53.  
  54. foreach $key (keys %id_list)
  55.   { print "+++ $key not completed +++\n$saved{$key}\n"; }
  56.  
  57. # End of exigrep
  58.